package com.cet.programmercarl.algorithmperformancanalysis.栈与队列;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * @program: algorithm
 * @description: 括号匹配问题
 * @author: 陈恩涛
 * @create: 2022-07-11 16:05
 **/
public class LC20 {

    public static void main(String[] args) {
        System.out.println(validatedParentheses("()[]{}"));
    }

    public static boolean validatedParentheses(String s) {
        final Deque<Character> deque = new ArrayDeque<>();
        final char[] charArray = s.toCharArray();
        for (char c : charArray) {
            if (c == '(' || c == '[' || c == '{') {
                deque.push(c);
            } else if (c == ')') {
                if (deque.isEmpty() || deque.pop() != '(') {
                    return false;
                }
            } else if (c == ']') {
                if (deque.isEmpty() || deque.pop() != '[') {
                    return false;
                }
            } else if (c == '}') {
                if (deque.isEmpty() || deque.pop() != '{') {
                    return false;
                }

            } else {
                throw new RuntimeException("输入的字符串不合法!");
            }
        }
        return deque.isEmpty();
    }
}
